package com.pac.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.pac.MyApp;
import com.pac.R;
import com.pac.bean.homepage.HomePageSlideItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/4/22.
 */
public class ViewPagerAdapter extends PagerAdapter {
    private Context context;
    private  List<HomePageSlideItem> datas;
    private List<View> views;

    public   ViewPagerAdapter(Context context, List<HomePageSlideItem> datas) {
        this.context = context;
        this.datas = datas;
        init();
    }

    private void init() {
        views=new ArrayList<>();
        views.clear();
        for (int i=0;i<datas.size();i++){
            if (datas==null){
                Toast.makeText(context,"请打开网络加载",Toast.LENGTH_SHORT).show();
            return;
            }
            View v= LayoutInflater.from(context).inflate(R.layout.home_viewpager,null);
            ImageView image= (ImageView) v.findViewById(R.id.home_viewpager_image);
            String img_url = datas.get(i).getHost_pic();
            Log.d("111",img_url);
            MyApp.bitmapUtils.display(image, img_url);
            TextView textView = (TextView) v.findViewById(R.id.home_viewpager_text);
            textView.setText(datas.get(i).getSubject());
            views.add(v);
        }
    }

    @Override
    public int getCount() {
        if (datas!=null){
            return datas.size();
        }
        return 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View v=views.get(position);
        container.addView(v);
        return v;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(views.get(position));
    }
}
